package init

import (
	"github.com/gin-gonic/gin/binding"
	"github.com/go-playground/validator/v10"
	"regexp"
)

func InitValidator() {
	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		err := v.RegisterValidation("validatorQQ", ValidatorQQ)
		if err != nil {
			return
		}
	}

	if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
		err := v.RegisterValidation("validatorName", ValidatorName)
		if err != nil {
			return
		}
	}

}

var ValidatorQQ validator.Func = func(fl validator.FieldLevel) bool {
	s := fl.Field().String()
	reg := regexp.MustCompile(`^\d{5,12}&`)
	return reg.MatchString(s)
}

var ValidatorName validator.Func = func(fl validator.FieldLevel) bool {
	s := fl.Field().String()
	reg := regexp.MustCompile(`^\w{2,20}&`)
	return reg.MatchString(s)
}
